package com.itheima.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.pagehelper.Page;
import com.itheima.domain.Book;
import com.itheima.domain.User;
import org.apache.ibatis.annotations.*;


public interface UserMapper extends BaseMapper<User> {
    @Select("select * from user where user_email=#{email} AND user_password=#{password}")
    @Results(id = "userMap",value = {
            //id字段默认为false，表示不是主键
            //column表示数据库表字段，property表示实体类属性名。
            @Result(id = true,column = "user_id",property = "id"),
            @Result(column = "user_name",property = "name"),
            @Result(column = "user_password",property = "password"),
            @Result(column = "user_email",property = "email"),
            @Result(column = "user_role",property = "role"),
            @Result(column = "user_status",property = "status")
    })
    User login(User user);

    @Select({"<script>" +
            "SELECT * FROM user " +
            "where user_role !='ADMIN'" +
            "<if test=\"name != null\"> AND  user_name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test=\"email != null\"> AND user_email like  CONCAT('%', #{email},'%') </if>" +
            "order by user_email" +
            "</script>"
    })
    @ResultMap("userMap")
        //分页查询用户
    Page<Book> searchUsers(User user);

    @Select({"<script>" +
            "SELECT * FROM user " +
            "where user_role !='ADMIN'" +
            "<if test=\"email != null\"> AND user_email = #{email} </if>" +
            "</script>"
    })
    @ResultMap("userMap")
    User selectByEmail(String email);
}
